package cn.witsky.rtp2ws.util;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

public class MapUtil {
	
	public static void main(String args[]) {
		Map<Integer,Long> map =new HashMap<Integer,Long>();
		map.put(3, 3000l);
		map.put(1, 1000l);
		map.put(2, 2000l);
		map.put(5, 3000l);
		map=MapUtil.sortMapByKey(map);
		Iterator<Entry<Integer,Long>> it = map.entrySet().iterator();
        while(it.hasNext()) {
            Entry<Integer,Long> entry = it.next();
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
	}
	
	public static Map<Integer,Long> sortMapByKey(Map<Integer,Long> map) {
		if (map == null || map.isEmpty()) {
			return null;
		}
		Map<Integer,Long> sortMap = new TreeMap<Integer,Long>(new MapKeyComparator());
		sortMap.putAll(map);
		return sortMap;
	}
}

class MapKeyComparator implements Comparator<Integer> {
	@Override
	public int compare(Integer str1, Integer str2) {
		if(str1<str2) {
			return -1;
		}
		return 1;
	}
}
